﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace asMeishi.web
{
    using Models;

    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new String[] { "asDigit.Controllers" }
            );
            routes.MapRoute(
                "_shop", // Route name
                "shop/detail/{id}/{channel}", // URL with parameters
                new { id = UrlParameter.Optional, channel = "mainContent", controller = "shop", action = "detail" }, // Parameter defaults
                new String[] { "asDigit.Controllers" }
            );

            routes.MapRoute(
                "expand", // Route name
                "{controller}/{action}/{id}/{p1}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional, p1 = UrlParameter.Optional }, // Parameter defaults
                new String[] { "asDigit.Controllers" }
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            publicDictApp pda = new publicDictApp();
            System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString() + "-- " + "Application_Start");
            pda.init("shopClass,shopAlly,shopPay,shopSize,shopHead,shopFit,shopUtilityTag,shopEnvironment");

            //一些环境赋 Application

            //成长体系
            setActionsApplication();
            

            userC.initUserDB();


        }
        public static asMeishi.web.Classes.baseConfig baseConfig = new asMeishi.web.Classes.baseConfig();

        protected void Session_Start()
        {
            baseConfig.setThemePathByIndex(1);
        } 

        /// <summary>
        /// 成长体系配置表 由数据库入Application
        /// </summary>
        public static void setActionsApplication()
        {
            var obj = new asMeishi.web.Models.DataSession<MongoDataContext, asMeishi.web.Models.actions>();

                HttpContext.Current.Application["asDigit.setting.actions"] = obj.query.ToList();

        }

        /// <summary>
        /// 得到 成长体系配置表 
        /// </summary>
        public static IEnumerable<asMeishi.web.Models.actions> getActionsApplication()
        {
            return HttpContext.Current.Application["asDigit.setting.actions"] as IEnumerable<asMeishi.web.Models.actions>;
        }

        /// <summary>
        /// 得到 成长体系配置 
        /// </summary>
        public static asMeishi.web.Models.actions getActionsApplication(asMeishi.web.Models.actionEnum act)
        {
            return getActionsApplication().SingleOrDefault(d => d.action == act);
        }

    }
    /// <summary>
    /// 将dict中的常用部分放在内存application中，好象可以实现自动更新
    /// </summary>
    public class publicDictApp
    {
        //食场总数
        public static long shopTotalCount = 0;
        public static long foodTotalCount = 0;

        public List<string> dictNames = new List<string>();
        public void init(string names)
        {
            dictNames.AddRange(names.Split(',').ToList());
            Dictionary<string, Dictionary<string, string>> _dict = new Dictionary<string, Dictionary<string, string>>();
            asMeishi.web.Models.dictClass dc = new asMeishi.web.Models.dictClass();
            foreach (var name in dictNames)
            {
                _dict.Add(name, asMeishi.web.Models.dictClass.get(name));
            }
            System.Web.HttpContext.Current.Cache["dict"] = _dict;

            System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString() + "-- " + "dict");

            shopC sc = new shopC();
            shopTotalCount = sc.sum();
            foodC fc = new foodC();
            foodTotalCount = fc.db.query.Count();
        }

        public void updata(string name)
        {
            var _dict = getDict();
            System.Web.HttpContext.Current.Cache["dict"] = _dict[name] = asMeishi.web.Models.dictClass.get("shopClass");

        }
        /// <summary>
        /// 取得name的字典值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Dictionary<string, string> get(string name)
        {
            var dictx = new Dictionary<string, string>();
            //用于dic表没有所有集合时 不报错
            try
            {
                dictx = (System.Web.HttpContext.Current.Cache["dict"] as Dictionary<string, Dictionary<string, string>>)[name];
            }
            catch {
                return new Dictionary<string, string>();
            }
            return dictx;
        }
        /// <summary>
        /// 取得dict总字典
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, Dictionary<string, string>> getDict()
        {
            return (System.Web.HttpContext.Current.Cache["dict"] as Dictionary<string, Dictionary<string, string>>);
        }

    }
}
